package com.easyj.jackson2;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.JsonNodeType;


public class StringDeserializer extends StdDeserializer<String> {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	StringDeserializer() {
        this(null);
    }

    StringDeserializer(Class<?> clazz) {
        super(clazz);
    }

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
        JsonNode node = jp.getCodec().readTree(jp);
        if (node.getNodeType() == JsonNodeType.STRING) {
            String val = node.asText("");
            if (StringUtils.isBlank(val)) {
                return null;
            }
        }

        return node.asText();
    }
}
